package com.cys.markdown.doc.admin.controller.sys;

import com.cys.common.data.RespCodeEnum;
import com.cys.common.pojo.RespEntity;
import com.cys.common.util.base.MapUtil;
import com.cys.common.util.base.StringUtil;
import com.cys.mid.core.system.interfaces.entity.ISysDept;
import com.cys.mid.core.system.interfaces.entity.ISysUser;
import com.cys.mid.core.system.interfaces.service.SysDeptService;
import com.cys.mid.core.system.interfaces.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门管理-控制层
 * Created by caiyushen on 2018/12/23.
 */
@Controller
@RequestMapping("/admin/sys/dept")
@Transactional(rollbackFor=Exception.class)
public class DeptController {

    private Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Resource(name = "sysDeptServiceImpl")
    private SysDeptService sysDeptService;

    @Resource(name = "sysUserServiceImpl")
    private SysUserService sysUserService;

    /**
     * 新增部门
     *
     * @param map 用户数据
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RespEntity add(@RequestParam Map map) throws Exception {
        if (!StringUtil.checkObj(map)) {
            logger.error("method[add] map is null.");
            return new RespEntity(RespCodeEnum.deficiency);
        }
        logger.info("method[add] map:" + map.toString());
        if (MapUtil.getString(map, "sysDeptParentId").equals("0")) {
            map.put("sysDeptParentId", null);
        }
        sysDeptService.save(map);
        return new RespEntity(RespCodeEnum.success);
    }

    /**
     * 修改部门
     *
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public RespEntity update(@RequestParam Map map) throws Exception {
        logger.info("method[update] map: " + map.toString());
        if (MapUtil.getString(map, "sysDeptParentId").equals("0")) {
            map.put("sysDeptParentId", null);
        }
        sysDeptService.update(map);
        return new RespEntity(RespCodeEnum.success);
    }

    /**
     * 根据部门id获取部门信息
     *
     * @param sysDeptId 部门id
     */
    @ResponseBody
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public RespEntity find(Long sysDeptId) {
        ISysDept sysDept = sysDeptService.find(sysDeptId);
        return new RespEntity(RespCodeEnum.success, sysDept);
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @author caiyushen
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public RespEntity delete(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[delete] id is null.");
            return new RespEntity(RespCodeEnum.deficiency);
        }
        //检查子部门数据
        List<ISysDept> childrenDeptList = sysDeptService.findListByParentId(id);
        if(!CollectionUtils.isEmpty(childrenDeptList)){
            return new RespEntity(RespCodeEnum.error,"请先删除子部门数据。");
        }

        //检查部门用户数据
        List<ISysUser> staffList = sysUserService.findListBySysDeptId(id);
        if(!CollectionUtils.isEmpty(staffList)){
            return new RespEntity(RespCodeEnum.error,"请先删除部门下的用户数据。");
        }
        //逻辑删除
        sysDeptService.logicDeleteById(id);
        return new RespEntity(RespCodeEnum.success);
    }

    /**
     * 获取部门树形结构
     */
    @ResponseBody
    @RequestMapping(value = "/getDeptTree", method = RequestMethod.GET)
    public RespEntity getDeptTree() {
        List<ISysDept> dataList = sysDeptService.findTreeList();
        List<Map> resultList = new ArrayList<>();
        for (ISysDept parentDept : dataList) {
            Map parentMap = new HashMap();
            parentMap.put("id", parentDept.getSysDeptId());
            parentMap.put("text", parentDept.getSysDeptName());
            if (!CollectionUtils.isEmpty(parentDept.getChildrenList())) {
                List<Map> childrenList = new ArrayList<>();
                for (ISysDept childrenDept : parentDept.getChildrenList()) {
                    Map childrenMap = new HashMap();
                    childrenMap.put("id", childrenDept.getSysDeptId());
                    childrenMap.put("text", childrenDept.getSysDeptName());
                    childrenList.add(childrenMap);
                }
                parentMap.put("children", childrenList);
            }
            resultList.add(parentMap);
        }
        return new RespEntity(RespCodeEnum.success, resultList);
    }

    /**
     * 获取所有一级部门
     */
    @ResponseBody
    @RequestMapping(value = "/getParentDeptList", method = RequestMethod.GET)
    public RespEntity getParentDeptList() {
        List<Map> resultList = new ArrayList<>();
        Map map = new HashMap();
        map.put("sysDeptId", "0");
        map.put("sysDeptName", "无");
        resultList.add(map);
        List<ISysDept> dataList = sysDeptService.findParentList();
        for (ISysDept sysDept : dataList) {
            Map deptMap = new HashMap();
            deptMap.put("sysDeptId", sysDept.getSysDeptId());
            deptMap.put("sysDeptName", sysDept.getSysDeptName());
            resultList.add(deptMap);
        }
        return new RespEntity(RespCodeEnum.success, resultList);
    }


}